package com.hns.can.mina.util;

import java.util.List;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilConfig;
import com.tencent.xinge.Message;
import com.tencent.xinge.XingeApp;

/**
 * @版权：HNS 版权所有 (c) 2013
 * @author:JeffXu
 * @version Revision 1.0.0
 * @email:835264047@qq.com
 * @see:
 * @创建日期：2015-4-27
 * @功能说明：腾讯信鸽推送工具类
 * @begin
 * @修改记录:
 * @修改后版本 修改人 修改内容
 * @2015-4-27 JeffXu 创建
 * @end
 */
public class XingeAppUtil {
    private Logger              logger       = LoggerFactory.getLogger(this.getClass());
    
    private static XingeAppUtil xingeAppUtil;
    
    private static long         ACCESSID  = UtilConfig.getLong("access_id");
    private static String       SECRETKEY = UtilConfig.getString("secret_key");
    
    /**
     * 实例化对象
     * 
     * @return
     */
    public static XingeAppUtil getInstance() {
        if (xingeAppUtil == null) {
            xingeAppUtil = new XingeAppUtil();
        }
        return xingeAppUtil;
    }
    
    /**
     * @param message
     * @param accountList
     * @return
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2015-4-27
     * @功能说明：推送给多个帐号
     */
    public JSONObject pushAccountList(Message message, List<String> accountList) throws Exception {
        if(PubUtil.isEmpty(ACCESSID) || PubUtil.isEmpty(SECRETKEY)){
            throw new BusinessException(MsgType.WARN.getFlag(), "与信鸽应用建立联系失败。");
        }
        XingeApp xinge = new XingeApp(ACCESSID, SECRETKEY);
        // logger.error("#Xinge    @与信鸽应用成功建立联系。。。");
        // message.setExpireTime(86400);
        // message.setType(Message.TYPE_MESSAGE);
        JSONObject ret = xinge.pushAccountList(0, accountList, message);
        
        String retCode = ret.get("ret_code").toString();
        if("0".equals(retCode)){
            logger.info("#Xinge    @消息推送成功。。。"+message);
        }else{
            logger.error("#Xinge    @消息推送失败。原因："+ret.get("err_msg").toString());
        }
        return (ret);
    }
    
    // 测试
    public static void main(String[] args) {
        
    }
    
}
